package com.songlanyun.modules.rabbitmq.mq.consumer;

import com.rabbitmq.client.Channel;
import com.songlanyun.common.enums.OrderConstant;
import com.songlanyun.common.utils.custom.ClassConvertUtils;
import com.songlanyun.modules.rabbitmq.DelayedConstant;
import com.songlanyun.modules.rabbitmq.mq.producer.SendOrderOrRightsOrderMsgService;
import com.songlanyun.modules.rights.entity.RightsOrder;
import com.songlanyun.modules.rights.service.RightsOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 维权申请商家过期没处理自动同意
 */
@Component
@Slf4j
public class AutoAgreeRightsConsumer extends BaseConsumer {

    @Autowired
    private RightsOrderService rightsOrderService;

    @Autowired
    private SendOrderOrRightsOrderMsgService sendOrderOrRightsOrderMsgService;
    @Override
    protected void process(Message message) {
        Long rightsId = ClassConvertUtils.objToLong(new String(message.getBody()));
        RightsOrder v = rightsOrderService.getById(rightsId);
        try {
            log.info(String.format("维权订单:%s,维权申请商家过期没处理，开始执行货自动同意", v.getRightsCode()));
            RightsOrder rightsOrder = rightsOrderService.autoAgree(v);
            if(OrderConstant.RightsType.RETURN.equals(v.getRightsType()))
            {
                sendOrderOrRightsOrderMsgService.rightsAgreeSendMsg(rightsOrder);
            }
            log.info(String.format("维权订单:%s,维权申请商家过期没处理，完成执行货自动同意", v.getRightsCode()));
        }
        catch (Exception ex) {
            log.error(String.format("维权订单:%s,维权申请商家过期没处理，完成执行货自动同意,出错%s", v.getOrderCode(), ex.getMessage()));
            throw  ex;
        }

    }

    @RabbitListener(queues = DelayedConstant.RightsAutoAgreeConstant.RIGHTS_AUTO_AGREE_DELAYED_QUEUE)
    public void consume(Message message, Channel channel) throws IOException {
        super.handle(message,channel);
    }
}
